package com.ztz.musicappdemo.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ztz.musicappdemo.MyDbHelper;
import com.ztz.musicappdemo.R;

public class UserChangeActivity extends BaseActivity {
    EditText edtUserName;
    EditText edtNewUserName;
    EditText edtPassword;
    EditText edtNewPassword;
    EditText edtNewPassword1;
    Button btnCancel;
    Button btnEnsure;
    MyDbHelper dbHelper;
    private static final String TAG = "UserChangeActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_change);
        dbHelper=new MyDbHelper(this,"UserInfoStore.db",null,1);
        edtUserName=(EditText)findViewById(R.id.edt_username);
        edtNewUserName=(EditText)findViewById(R.id.edt_new_username);
        edtPassword=(EditText)findViewById(R.id.edt_password);
        edtNewPassword=(EditText)findViewById(R.id.edt_new_password);
        edtNewPassword1=(EditText)findViewById(R.id.edt_new_password1);
        btnCancel=(Button)findViewById(R.id.btn_cancel);
        btnEnsure=(Button)findViewById(R.id.btn_ensure);
        final Intent intent=getIntent();
//        String userName=intent.getStringExtra("userName");
//        String password=intent.getStringExtra("password");
//        edtUserName.setText(userName);
//        edtPassword.setText(password);
//        Log.d(TAG,"用户名 "+userName);
//        Log.d(TAG,"密码 "+password);
//        SQLiteDatabase db=dbHelper.getWritableDatabase();
//        Cursor cursor=db.query("UserInfo",null,"userName=?",new String[]{"ztz"},null,null,null);
//        while (cursor.moveToNext()){
//            String userName=cursor.getString(cursor.getColumnIndex("userName"));
//            String password=cursor.getString(cursor.getColumnIndex("password"));
//            edtUserName.setText(userName);
//            edtPassword.setText(password);
//        }

        btnEnsure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String edtName=edtUserName.getText().toString();
                String edtpassword=edtPassword.getText().toString();
                String userName=edtNewUserName.getText().toString();
                String pwd1=edtNewPassword.getText().toString();
                String pwd2=edtNewPassword1.getText().toString();
                if(edtName.isEmpty()||edtpassword.isEmpty()||userName.isEmpty()||pwd1.isEmpty()||pwd2.isEmpty()){
                    Toast.makeText(UserChangeActivity.this, "请重新输入", Toast.LENGTH_SHORT).show();
                }else if(pwd1.equals(pwd2)){
                    SQLiteDatabase db=dbHelper.getWritableDatabase();
                    ContentValues values=new ContentValues();
                    values.put("userName",userName);
                    values.put("password",pwd1);
                    db.update("UserInfo",values,"userName=?",new String[]{edtName});
                    Toast.makeText(UserChangeActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    Intent intent1=new Intent(UserChangeActivity.this,LoginActivity.class);
                    startActivity(intent1);
                }else {
                    Toast.makeText(UserChangeActivity.this, "两次密码输入不一样,请重新输入", Toast.LENGTH_SHORT).show();
                    edtNewPassword.setText("");
                    edtNewPassword1.setText("");
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(UserChangeActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

    }
}
